<template>
	<div class="datamonitor">
		<div class="head">
			<span>姓名: {{infoData.real_name}}</span>
			<span>性别: {{infoData.gender}}</span>
			<span>年龄: {{infoData.age}}</span>
			<span>联系方式: {{infoData.tel}}</span>
		</div>
		<v-table ref='vTable' :tableColumns="tableColumns" :tableData="tableData"></v-table>
	</div>
</template>

<script>
	import vTable from '@/components/common/Table.vue';
	export default {
		components: {
			vTable
		},
		data(){
			return {
				customer_id: '',
				infoData: {
					real_name: '',
					gender: '',
					age: '',
					tel: ''
				},
				tableColumns: [
					{ type: "index", align: "center", width: 60 },
			        { title: "测量类型", key: "name", align: "center" },
			        { title: "测量值", key: "number", align: "center",
			        	render: (h, params) => {
//			        		console.log(params.row)
			        		if(params.row.number > 10){
			        			return h('span',{
									style: { color: 'red' }
								},params.row.number)
			        		}else {
								return  h('span', params.row.number);
							}
			        	}
			        },
			        { title: "最新测量时间", key: "time", align: "center" },
			        {
                        title: '操作',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: { type: 'primary', size: 'small' },
                                    style: { marginRight: '5px' },
                                    on: {
                                        click: () => {
//                                      	console.log(params.row)
											console.log(this.infoData)
                                        	if(params.row.name === '血糖') {
                                        		this.$router.push({
                                        			path: '/user/monitor/bloodsugar/edit/' + this.customer_id,
                                        			query: {obj: this.infoData}
                                        		})
                                        	} else if(params.row.name === '收缩压' || params.row.name === '舒张压' || params.row.name === '心率'){
                                        		this.$router.push({
                                        			path: '/user/monitor/bloodpre/edit/' + this.customer_id,
                                        			query: {obj: this.infoData}
                                        		})
                                        	} else {
                                        		this.$router.push({
                                        			path: '/user/monitor/weight/edit/' + this.customer_id,
                                        			query: {obj: this.infoData}
                                        		})
                                        	}
                                        }
                                    }
                                }, '历史数据')
                            ]);
                        }
                    }
				],
				tableData: []
			}
		},
		methods:{
			// 获取数据
			getData(){
				this.$http.get('/customer_monitor/last_monitor/' + this.customer_id)
				.then(({data})=>{
					console.log(data)
					// 基本信息
					this.infoData.real_name = data.data.real_name;
					if(data.data.gender == 1) {
						this.infoData.gender = '男'
					} else {
						this.infoData.gender = '女'
					}
					this.infoData.age = data.data.age;
					this.infoData.tel = data.data.tel;
					//  舒张压 
					let obj1 = {};
					obj1.name = '舒张压';
					obj1.number = data.data.diastolicpressure;
					obj1.time= new Date(parseInt(data.data.p_test_time)).toLocaleDateString()
					if(data.data.diastolicpressure !== undefined){
						this.tableData.push(obj1);
					}
					
					
					//  收缩压
					let obj2 = {};
					obj2.name = '收缩压';
					obj2.number = data.data.systolicpressure;
					//  时间戳转日期
					obj2.time= new Date(parseInt(data.data.p_test_time)).toLocaleDateString()
					if(data.data.systolicpressure !== undefined){
						this.tableData.push(obj2);
					}
					
					
					//  心率
					let obj3 = {};
					obj3.name = '心率';
					obj3.number = data.data.p;
					//  时间戳转日期
					obj3.time= new Date(parseInt(data.data.p_test_time)).toLocaleDateString()
					if(data.data.p !== undefined){
						this.tableData.push(obj3);
					}
					
					//  血糖
					let obj4 = {};
					obj4.name = '血糖';
					obj4.number = data.data.glu;
					//  时间戳转日期
					obj4.time= new Date(parseInt(data.data.glu_test_time)).toLocaleDateString()
					if(data.data.glu !== undefined){
						this.tableData.push(obj4);
					}
					
					//  体重
					let obj5 = {};
					obj5.name = '体重';
					obj5.number = data.data.weight;
					//  时间戳转日期
					obj5.time= new Date(parseInt(data.data.weight_test_time)).toLocaleDateString()
					if(data.data.weight !== undefined) {
						this.tableData.push(obj5);
					}
//					console.log(this.tableData)
				})
				
				.catch((err)=>{
					console.log(err);
				})
			}
		},
		created(){
			// 获取id
			this.customer_id = this.$route.params.id;
			this.getData();
			console.log(this.$route.params.id)
		}
	}
</script>

<style lang="less" scoped>
.datamonitor {
	position: relative;
	width: 100%;
	height: 100%;
	.head {
		margin-bottom: 20px;
		span{
			margin-left: 50px;
		}
	}
}

</style>